<?php
namespace app\index\controller;
use think\Controller;

class Login extends controller{
    function index(){
		return $this->fetch();
	}
	function sta(){
		$result = model('user')->ss(session('userid'));
		if($result['tiren']==0){
			model('user')->where('id',session('userid'))->update(['status'=>0]);
			return false;
		}
		return true;
	}
	function logout(){
		session("client",null);
		model('user')->where('id',session('userid'))->update(['status'=>0]);
		$this->redirect("index/index/index");
	}
	function login($code){
		if(!captcha_check($code)){
			 //验证失败
			$this->error('验证码错误');
			}
		$username = $_POST['username'];
		$password = $_POST['password'];

		$judge = Model("User")->findall($username,$password);
		if($judge == true){
			if($judge["tiren"]==1){
				session('client',$username);
				session('userid',$judge['id']);
				$last = $judge["intime"];
				$n = $judge['numbers'];
				$n++;
				$userip = request()->ip();
				Model('user')->where("id",$judge['id'])->update(['numbers'=>$n,'intime'=>time(),'last_time'=>$last,"ipaddrs"=>$userip,'status'=>1]);
				return $this->redirect("index/home/index");
				exit;
			}else{
				return $this->redirect("index/login/logout");
			}
		}
		$this->error("登录错误!");
	}
}
